package com.jijuxie;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class UpdateCoverImages {
    public static void main(String[] args) {
        try {
            // 加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            // 获取数据库连接
            String url = "jdbc:mysql://localhost:3306/jijuxie?useUnicode=true&characterEncoding=utf8";
            String username = "root"; // 替换为您的数据库用户名
            String password = ""; // 替换为您的数据库密码
            
            Connection conn = DriverManager.getConnection(url, username, password);
            
            // 创建Statement对象
            Statement stmt = conn.createStatement();
            
            // 执行SQL语句
            String sql = "UPDATE house_image hi1 SET hi1.image_type = '1' " +
                        "WHERE hi1.image_id = (SELECT MIN(hi2.image_id) " +
                        "FROM (SELECT * FROM house_image) hi2 " +
                        "WHERE hi2.house_id = hi1.house_id)";
            
            int rowsAffected = stmt.executeUpdate(sql);
            
            System.out.println("更新成功！受影响的行数: " + rowsAffected);
            
            // 关闭资源
            stmt.close();
            conn.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 